package com.hj.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hj.dao.CompanyMapper;
import com.hj.dao.SingleStopMapper;
import com.hj.entity.Company;
import com.hj.entity.Ss_accesscode;
import com.hj.entity.Ss_accesscodecopy;
import com.hj.service.SingleStopService;
import com.hj.utils.MultipleDataSource;

@Service("singleStopService")
@Transactional(value="txManager")
public class SingleStopServiceImpl implements SingleStopService {
	@Autowired
	SingleStopMapper singleStopMapper;
	@Autowired
	private CompanyMapper companymapper;
	@Override
	public String singleStop(String single400) {
		String vccId = null, json1 = null;
		if (single400.length() > 4) { // 后面通过正则判断
			MultipleDataSource.setDataSourceKey("dataSource1");
			Company cp = companymapper.search_cp2(single400);
			if (cp != null && !cp.equals("")) {
				json1 = "停机成功!号码类别:" + cp.getCategory() + ","
						+ "低消:" + cp.getMonth_fee();

				companymapper.update_cp(single400); 

				vccId = single400.substring(4);
				MultipleDataSource.setDataSourceKey("dataSource");
				List<Ss_accesscode> s_ac = singleStopMapper.search_ac(vccId);
				
				if (s_ac != null && !s_ac.equals("")) {
					// 判断备份表是否存在该400号码
					MultipleDataSource.setDataSourceKey("dataSource1");
					Ss_accesscodecopy exist_cp = singleStopMapper.search_exist(vccId);
					if (exist_cp == null) {
						for(Ss_accesscode sac:s_ac)
						singleStopMapper.insert_cp(sac);
					}else{
					// 更新备份表的400号码接入策略
					for(Ss_accesscode sac:s_ac)
					singleStopMapper.update_copy(sac);
					}
					MultipleDataSource.setDataSourceKey("dataSource");
					// 执行停机操作
					singleStopMapper.update_stop(vccId);
					singleStopMapper.update_cp(vccId);
				}
				
				List<Ss_accesscode> sac = singleStopMapper.search_stop(vccId);
				String serviceId = sac.get(0).getServiceId();
				if (serviceId.equals("000001999999004444")) {
					return json1;
				}else{
					return json1="停机不成功,请重试!";
				}
			} else {
				return json1 = "400号码不存在!";
			}
		} else {
			return json1 = "400号码不正确,请重新输入400号码!";
		}
	}

}
